package indi.tshoiasc.simstr.config;

import indi.tshoiasc.simstr.SimStrengthMain;
import indi.tshoiasc.simstr.utils.GeneralOperate;
import indi.tshoiasc.simstr.utils.ItemOperate;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

public class ConfigLoader {
	public static FileConfiguration config;
	public static YamlConfiguration itemyml = new YamlConfiguration();
	public static YamlConfiguration costitemyml = new YamlConfiguration();
	public static Map<String, ItemStack> itemMap = new HashMap<String, ItemStack>();
	public static Map<Integer, ConfigHolder> list = new HashMap<Integer, ConfigHolder>();

	public void loadConfig(SimStrengthMain plugin) {
		try {
			File configFile = new File(plugin.getDataFolder(), "config.yml");
			if (!configFile.exists()) {
				plugin.saveDefaultConfig();
			}
			config = plugin.getConfig();
		} catch (Exception e) {
			e.printStackTrace();
		}
		File itemFile = new File(plugin.getDataFolder(), "item.yml");
		if (!itemFile.exists()) {
			plugin.saveResource("item.yml", false);
		}
		itemyml = YamlConfiguration.loadConfiguration(itemFile);

		File costitemFile = new File(plugin.getDataFolder(), "costitem.yml");
		if (!costitemFile.exists()) {
			plugin.saveResource("costitem.yml", false);
		}
		costitemyml = YamlConfiguration.loadConfiguration(costitemFile);
		toConfigHolder();
	}

	public static void toConfigHolder() {
		List<String> s = config.getStringList("Open");
		for (int i = 0; i < s.size(); i++) {
			int ID = itemyml.getInt((String) s.get(i) + ".id");
			String name = itemyml.getString((String) s.get(i) + ".defaultname");
			int Mode = itemyml.getInt((String) s.get(i) + ".mode");
			int normal_CostMoney = itemyml.getInt((String) s.get(i) + ".normal.costMoney");
			List<Integer> normal_Random = itemyml.getIntegerList((String) s.get(i) + ".normal.random");

			List<String> normal_CostItem = itemyml.getStringList((String) s.get(i) + ".normal.costItem");
			List<ItemStack> itemstackArray_normal = new ArrayList<ItemStack>();
			for (String str : normal_CostItem) {
				String[] itemArray = str.split(" ");
				if (itemMap.containsKey(itemArray[0])) {
					ItemStack im = ((ItemStack) itemMap.get(itemArray[0])).clone();
					im.setAmount(Integer.valueOf(itemArray[1]).intValue());
					itemstackArray_normal.add(im);
				} else {
					int tempID = costitemyml.getInt(itemArray[0] + ".id");
					String tempName = costitemyml.getString(itemArray[0] + ".name");
					List<String> tempLore = costitemyml.getStringList(itemArray[0] + ".lore");
					ItemStack temp = ItemOperate.generateItem(tempID, tempName, tempLore,
							Integer.valueOf(itemArray[1]));
					temp.setAmount(Integer.valueOf(itemArray[1]).intValue());
					itemstackArray_normal.add(temp);
					itemMap.put(itemArray[0], temp.clone());
				}
			}
			int luck_CostMoney = itemyml.getInt((String) s.get(i) + ".luck.addMoney");
			int luck_addRandom = itemyml.getInt((String) s.get(i) + ".luck.addRandom");
			String luck_CostItem = itemyml.getString((String) s.get(i) + ".luck.costItem");
			ItemStack itemstackArray_luck;
			List<String> tempLore2;

			if (itemMap.containsKey(luck_CostItem)) {
				ItemStack im = ((ItemStack) itemMap.get(luck_CostItem)).clone();
				im.setAmount(1);
				itemstackArray_luck = im;
			} else {
				int tempID = costitemyml.getInt(luck_CostItem + ".id");
				String tempName = costitemyml.getString(luck_CostItem + ".name");
				tempLore2 = costitemyml.getStringList(luck_CostItem + ".lore");
				ItemStack temp1 = ItemOperate.generateItem(tempID, tempName, tempLore2, 1);
				itemstackArray_luck = temp1;
				itemMap.put(luck_CostItem, temp1);
			}

			int safe_CostMoney = itemyml.getInt((String) s.get(i) + ".safe.addMoney");
			String safe_CostItem = itemyml.getString((String) s.get(i) + ".safe.costItem");
			ItemStack itemstackArray_safe;

			String[] itemArray1 = safe_CostItem.split(" ");
			if (itemMap.containsKey(itemArray1[0])) {
				ItemStack im = ((ItemStack) itemMap.get(safe_CostItem)).clone();
				im.setAmount(1);
				itemstackArray_safe = im;
			} else {
				int tempID = costitemyml.getInt(safe_CostItem + ".id");
				String tempName3 = costitemyml.getString(safe_CostItem + ".name");
				List<String> tempLore3 = costitemyml.getStringList(safe_CostItem + ".lore");
				ItemStack temp2 = ItemOperate.generateItem(tempID, tempName3, tempLore3, 1);
				itemstackArray_safe = temp2;
				itemMap.put(itemArray1[0], temp2);
			}

			ConfigHolder ch = new ConfigHolder(ID, name, Mode, normal_CostMoney, itemstackArray_normal, normal_Random,
					luck_CostMoney, itemstackArray_luck, luck_addRandom, safe_CostMoney, itemstackArray_safe);
			list.put(Integer.valueOf(ID), ch);
		}
		ConfigHolder.Broadcast_content = config.getString("Broadcast.content");
		ConfigHolder.Broadcast_level = config.getInt("Broadcast.level");
		ConfigHolder.Cooldown = config.getInt("Cooldown");
		ConfigHolder.addValue = config.getDouble("Strength.addValue");
		ConfigHolder.successEffect = GeneralOperate.getEffect(config.getInt("Strength.successEffect"));
		ConfigHolder.failEffect = GeneralOperate.getEffect(config.getInt("Strength.failEffect"));
	}
}
